package com.ygo.feihua;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import com.ygo.feihua.BmobTable.ZhuJian;
import com.ygo.feihua.BmobTable.Zhuti;
import com.ygo.feihua.view.Advertisements;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.view.MenuItem;
import android.text.InputType;
import android.widget.ArrayAdapter;
import android.graphics.Color;
import android.widget.ProgressBar;
import com.ygo.feihua.util.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.view.*;

public class ZhutiZhuye extends AppCompatActivity implements OnClickListener
{
	
	private LinearLayout llAdvertiseBoard;
	private LayoutInflater inflater;	
	Gongju gj;
	private DialogUtils du;
	LayoutInflater ll;

	private GridView zy_gv;
	private ProgressBar zy_pro;
	private Button zy_cz;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.zhuti_zhuye);
		
		SdkInitManagement.getDx().initBmobSdk();
		
		gj=Gongju.getdx(ZhutiZhuye.this);
	
		du=DialogUtils.getdx(this);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);	
		
		gj.cshToolbar(toolbar,"主题下载");		
		//OurBar ourbar=(OurBar) findViewById(R.id.ourbar);
		setSupportActionBar(toolbar);
		toolbar.setTitle("主题下载");
		//toolbar.initMenu();
		toolbar.setOnMenuItemClickListener(onMenuItemClick);
		
		
		csh();
		
		gv();

		inflater = LayoutInflater.from(ZhutiZhuye.this);
		initViews();

		
		zy_cz.setOnClickListener(this);
	}
	
	private void csh(){
		
		zy_gv=(GridView) findViewById(R.id.zt_zt);
		zy_pro=(ProgressBar) findViewById(R.id.zy_pro);
		zy_cz=(Button) findViewById(R.id.zy_cz);
		
		zy_cz.setVisibility(View.GONE);
		zy_gv.setVisibility(View.GONE);
		zy_pro.setVisibility(View.VISIBLE);
		
		
	}
	
	
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{

			
			switch(p1.getItemId()){
				//已下载主题
				case R.id.zt_download:
					startActivity(new Intent(ZhutiZhuye.this,ZhutiXialie.class));
					break;
				//搜索主题
				case R.id.zt_ss:
					sszt();
					break;
			}
			
			
			// TODO: Implement this method
			return true;
		}
	};
	private void sszt(){
		View[] v=du.dialoge(true,"搜索主题","请输入主题名称");
		final EditText ed=(EditText) v[0];
		Button bt=(Button) v[1];
		//设置输入文本
		ed.setInputType(InputType.TYPE_CLASS_TEXT);
		//设置回车搜索
		ed.setOnEditorActionListener(new TextView.OnEditorActionListener() {
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {

						String keytag = ed.getText().toString().trim();

						if (keytag.equals("")) {
							gj.show("关键字不能为空");
							return false;
						}

						du.dis();
						// 搜索功能主体
						Intent intent =new Intent(ZhutiZhuye.this,ZhutiZhuxian.class);
						intent.putExtra("zt",keytag);
						startActivity(intent);
						return true;
					}
					return false;
				}
			});
		bt.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String gz=ed.getText().toString();
					if(gz.equals("")){
						gj.show("关键字不能为空");
					}else{
						du.dis();
						Intent intent =new Intent(ZhutiZhuye.this,ZhutiZhuxian.class);
						intent.putExtra("zt",gz);
						startActivity(intent);
					}
					// TODO: Implement this method
				}
			});
		
	}
		
	
	
//表格布局初始化
	public void gv(){	
	
		BmobQuery<Zhuti> ztt=new BmobQuery<Zhuti>();
		ztt.addWhereEqualTo("ztsh",1);
		ztt.setLimit(50);
		ztt.addWhereEqualTo("vercode",Gongju.ZHUTI_VERCODE);
		ztt.order("-createdAt");
		ztt.findObjects(new FindListener<Zhuti>(){

				@Override
				public void done(List<Zhuti> p1, BmobException p2)
				{
					if(p2==null){
					if (p1.size() == 0)
					{
						zy_gv.setVisibility(View.GONE);
						zy_pro.setVisibility(View.GONE);
						zy_cz.setVisibility(View.GONE);
						gj.show("没有主题喵~");
					}else{							
						zy_gv.setAdapter(new gvitem(ZhutiZhuye.this, p1));
						zy_gv.setVisibility(View.VISIBLE);
						zy_pro.setVisibility(View.GONE);
						zy_cz.setVisibility(View.GONE);
						
					}
					// TODO: Implement this method
				}else{
					zy_gv.setVisibility(View.GONE);
					zy_pro.setVisibility(View.GONE);
					zy_cz.setVisibility(View.VISIBLE);
					}
				}
				
				
				
				});

		}
	
	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.zy_cz:
				zy_cz.setVisibility(View.GONE);
				zy_gv.setVisibility(View.GONE);
				zy_pro.setVisibility(View.VISIBLE);
				gv();
				break;
		}
		
		// TODO: Implement this method
	}

	private void initViews()
	{
		llAdvertiseBoard = (LinearLayout)findViewById(R.id.llAdvertiseBoard);


		// 添加图片的Url地址
		BmobQuery<ZhuJian> zj=new BmobQuery<ZhuJian>();
			zj.setLimit(50);
			zj.order("-createdAt");
		zj.findObjects(new FindListener<ZhuJian>(){

				@Override
				public void done(List<ZhuJian> p1, BmobException p2)
				{
					if(p2==null){
						if (p1.size() != 0)
						{
							llAdvertiseBoard.addView(new Advertisements(ZhutiZhuye.this, true,  3000, 1).initView((List<Object>)p1));
						}			
					}
					// TODO: Implement this method
				}
				});
		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method

		getMenuInflater().inflate(R.menu.menu_zhuti, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
		
		
}
